<template>
  <el-dialog
    :title="!dataForm.rentHouseid ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="地区id" prop="mapid">
      <el-input v-model="dataForm.mapid" placeholder="地区id"></el-input>
    </el-form-item>
    <el-form-item label="租房方式(1.整租 2.合租)" prop="rentHousemode">
      <el-input v-model="dataForm.rentHousemode" placeholder="租房方式(1.整租 2.合租)"></el-input>
    </el-form-item>
    <el-form-item label="租房租金" prop="rentHouseprice">
      <el-input v-model="dataForm.rentHouseprice" placeholder="租房租金"></el-input>
    </el-form-item>
    <el-form-item label="租房房型(1.一室 2.二室 3.三室)" prop="rentHouseroom">
      <el-input v-model="dataForm.rentHouseroom" placeholder="租房房型(1.一室 2.二室 3.三室)"></el-input>
    </el-form-item>
    <el-form-item label="租房朝向(1.朝东 2.朝南 3.朝西 4.朝北)" prop="rentHouseorie">
      <el-input v-model="dataForm.rentHouseorie" placeholder="租房朝向(1.朝东 2.朝南 3.朝西 4.朝北)"></el-input>
    </el-form-item>
    <el-form-item label="租房租期(1.月租 2.季租 3.年租)" prop="rentHousedate">
      <el-input v-model="dataForm.rentHousedate" placeholder="租房租期(1.月租 2.季租 3.年租)"></el-input>
    </el-form-item>
    <el-form-item label="租房楼层(1.低楼层 2.中楼层 3.高楼层)" prop="rentHousefloor">
      <el-input v-model="dataForm.rentHousefloor" placeholder="租房楼层(1.低楼层 2.中楼层 3.高楼层)"></el-input>
    </el-form-item>
    <el-form-item label="租房特色(1.近地铁 2.环境好 3.低价)" prop="rentHousefeature">
      <el-input v-model="dataForm.rentHousefeature" placeholder="租房特色(1.近地铁 2.环境好 3.低价)"></el-input>
    </el-form-item>
    <el-form-item label="租房图片" prop="rentHouseimg">
      <el-input v-model="dataForm.rentHouseimg" placeholder="租房图片"></el-input>
    </el-form-item>
    <el-form-item label="租房简介" prop="rentHousesyno">
      <el-input v-model="dataForm.rentHousesyno" placeholder="租房简介"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          rentHouseid: 0,
          mapid: '',
          rentHousemode: '',
          rentHouseprice: '',
          rentHouseroom: '',
          rentHouseorie: '',
          rentHousedate: '',
          rentHousefloor: '',
          rentHousefeature: '',
          rentHouseimg: '',
          rentHousesyno: ''
        },
        dataRule: {
          mapid: [
            { required: true, message: '地区id不能为空', trigger: 'blur' }
          ],
          rentHousemode: [
            { required: true, message: '租房方式(1.整租 2.合租)不能为空', trigger: 'blur' }
          ],
          rentHouseprice: [
            { required: true, message: '租房租金不能为空', trigger: 'blur' }
          ],
          rentHouseroom: [
            { required: true, message: '租房房型(1.一室 2.二室 3.三室)不能为空', trigger: 'blur' }
          ],
          rentHouseorie: [
            { required: true, message: '租房朝向(1.朝东 2.朝南 3.朝西 4.朝北)不能为空', trigger: 'blur' }
          ],
          rentHousedate: [
            { required: true, message: '租房租期(1.月租 2.季租 3.年租)不能为空', trigger: 'blur' }
          ],
          rentHousefloor: [
            { required: true, message: '租房楼层(1.低楼层 2.中楼层 3.高楼层)不能为空', trigger: 'blur' }
          ],
          rentHousefeature: [
            { required: true, message: '租房特色(1.近地铁 2.环境好 3.低价)不能为空', trigger: 'blur' }
          ],
          rentHouseimg: [
            { required: true, message: '租房图片不能为空', trigger: 'blur' }
          ],
          rentHousesyno: [
            { required: true, message: '租房简介不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.rentHouseid = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.rentHouseid) {
            this.$http({
              url: this.$http.adornUrl(`/house/renthouse/info/${this.dataForm.rentHouseid}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.mapid = data.renthouse.mapid
                this.dataForm.rentHousemode = data.renthouse.rentHousemode
                this.dataForm.rentHouseprice = data.renthouse.rentHouseprice
                this.dataForm.rentHouseroom = data.renthouse.rentHouseroom
                this.dataForm.rentHouseorie = data.renthouse.rentHouseorie
                this.dataForm.rentHousedate = data.renthouse.rentHousedate
                this.dataForm.rentHousefloor = data.renthouse.rentHousefloor
                this.dataForm.rentHousefeature = data.renthouse.rentHousefeature
                this.dataForm.rentHouseimg = data.renthouse.rentHouseimg
                this.dataForm.rentHousesyno = data.renthouse.rentHousesyno
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/house/renthouse/${!this.dataForm.rentHouseid ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'rentHouseid': this.dataForm.rentHouseid || undefined,
                'mapid': this.dataForm.mapid,
                'rentHousemode': this.dataForm.rentHousemode,
                'rentHouseprice': this.dataForm.rentHouseprice,
                'rentHouseroom': this.dataForm.rentHouseroom,
                'rentHouseorie': this.dataForm.rentHouseorie,
                'rentHousedate': this.dataForm.rentHousedate,
                'rentHousefloor': this.dataForm.rentHousefloor,
                'rentHousefeature': this.dataForm.rentHousefeature,
                'rentHouseimg': this.dataForm.rentHouseimg,
                'rentHousesyno': this.dataForm.rentHousesyno
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
